package com.antoco.operation.bigscreen.service.autogeneration.impl;

import com.antoco.operation.bigscreen.model.entity.TjIns;
import com.antoco.operation.bigscreen.repository.SensorNumTjRepository;
import com.antoco.operation.bigscreen.model.entity.SensorNumTj;
import com.antoco.operation.bigscreen.service.autogeneration.intf.SensorNumTjService;
import com.antoco.operation.bigscreen.utils.BeanUtils;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;

@Service
@Transactional
public class SensorNumTjServiceImpl extends AbstractModelServiceImpl implements SensorNumTjService {
    private final SensorNumTjRepository repository;

    public SensorNumTjServiceImpl(SensorNumTjRepository repository) {
        this.repository = repository;
    }

    @Override
    public SensorNumTj save(SensorNumTj entity) {
        //创建统计实例/更新统计主题
        TjIns tjIns = BeanUtils.saveTjIns(SensorNumTj.class);
        //设置tj_ins_id
        entity.setTjInsId(tjIns.getId());
        return repository.save(entity);
    }

    @Override
    public List<SensorNumTj> save(List<SensorNumTj> entities) {
        TjIns tjIns = BeanUtils.saveTjIns(SensorNumTj.class);
        for (SensorNumTj sensorNumTj : entities) {
            sensorNumTj.setTjInsId(tjIns.getId());
        }
        return (List<SensorNumTj>) repository.saveAll(entities);
    }

    @Override
    public void deleteById(String id) {
        repository.deleteById(id);
    }

    @Override
    public Optional<SensorNumTj> findById(String id) {
        return repository.findById(id);
    }

    @Override
    public List<SensorNumTj> findAll() {
        return (List<SensorNumTj>) repository.findAll();
    }

    @Override
    public Page<SensorNumTj> findAll(Pageable pageable) {
        Page<SensorNumTj> entityPage = repository.findAll(pageable);
        List<SensorNumTj> entities = entityPage.getContent();
        return new PageImpl<>(entities, pageable, entityPage.getTotalElements());
    }

    @Override
    public SensorNumTj update(SensorNumTj entity, String id) {
        Optional<SensorNumTj> optional = findById(id);
        if (optional.isPresent()) {
            return save(entity);
        }
        return null;
    }
}